//
//  CustomerView.m
//  AoJi
//
//  Created by IMacLife on 15/2/3.
//  Copyright (c) 2015年 xuanheng. All rights reserved.
//
//个人客户

#import "CustomerView.h"


@implementation CustomerView

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self loadView];
        [self loadData];
    }
    return self;
}

//加载view
-(void)loadView
{
    _tableview = [[UITableView alloc]initWithFrame:self.bounds style:UITableViewStylePlain];
    _tableview.delegate=self;
    _tableview.dataSource=self;
    _tableview.backgroundColor=[UIColor clearColor];
    [self addSubview:_tableview];
    
    UIView *footView = [[UIView alloc]init];
    footView.backgroundColor=[Color colorFromHexRGB:@"e4e4e4"];
    _tableview.tableFooterView=footView;
}

-(void)loadData
{
    //Url：http://cent.dsgrd.com/me/customer?page=<page>
    //参数：page，可选，分页参数，从0 开始，默认为第一页(page=0)
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"page"]=@"0";
    
    [YHNetwork getInformationWithReguestType:RequestTypePost WithParameters:param WithApi:@"me/customer" WhenSuccess:^(NSMutableDictionary *response) {
        NSLog(@"%@",response);
        dataArray = response;
        [_tableview reloadData];
    } WhenFailure:^(NSError *failure) {
        NSLog(@"%@",failure);
    }];
}

#pragma mark ----------------UITableViewDelegate
//一个分区显示多少行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return dataArray.count;
}


//UITableViewCell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *identifier = @"cell";
    UITableViewCell *cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.selectionStyle=UITableViewCellSelectionStyleNone;
    
    UILabel *name = [[UILabel alloc]initWithFrame:CGRectMake(5, 5, 100, 25)];
    name.text = dataArray[indexPath.row][@"name"];
    [cell.contentView addSubview:name];
    
    UILabel *reason = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width/2-50, 5, 100, 25)];
    reason.text = dataArray[indexPath.row][@"reason"];
    reason.font = [UIFont systemFontOfSize:14];
    reason.textAlignment=NSTextAlignmentCenter;
    [cell.contentView addSubview:reason];
    
    UILabel *phone = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width-80, 5, 100, 25)];
    phone.text = dataArray[indexPath.row][@"phone_mask"];
    phone.font = [UIFont systemFontOfSize:13];
    [cell.contentView addSubview:phone];
    
    UIView *line = [[UIView alloc]initWithFrame:CGRectMake(0, 30, tableView.frame.size.width, 1)];
    line.backgroundColor=[Color colorFromHexRGB:@"f0f0f0"];
    [cell.contentView addSubview:line];
    
    UILabel *number = [[UILabel alloc]initWithFrame:CGRectMake(5, 36, 200, 20)];
    number.text = dataArray[indexPath.row][@"number"];
    number.font = [UIFont systemFontOfSize:12];
    [cell.contentView addSubview:number];
    
    UILabel *status = [[UILabel alloc]initWithFrame:CGRectMake(tableView.frame.size.width/2-25, 36, 50, 20)];
    status.textColor=[UIColor whiteColor];
    status.textAlignment=NSTextAlignmentCenter;
    status.font = [UIFont systemFontOfSize:12];
    
    switch ([dataArray[indexPath.row][@"status"]intValue]) {
        case 0:
            status.text=@"未付款";
            status.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"weifukuan"]];
            break;
        case 1:
            status.text=@"已付款";
            status.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"yifukuan"]];
            break;
            
        default:
            break;
    }
    [cell.contentView addSubview:status];
    
    
    UIButton *contact = [UIButton buttonWithType:UIButtonTypeCustom];
    contact.frame = CGRectMake(tableView.frame.size.width-55, 36, 50, 20);
    contact.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"lianxiwo"]];
    [contact addTarget:self action:@selector(contactMe) forControlEvents:UIControlEventTouchUpInside];
    [cell.contentView addSubview:contact];
    
    UIView *spacing = [[UIView alloc]initWithFrame:CGRectMake(0, 60, tableView.frame.size.width, 5)];
    spacing.backgroundColor=[Color colorFromHexRGB:@"f0f0f0"];
    [cell.contentView addSubview:spacing];
    
    return cell;
}


//cell行高
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 65;
}

#pragma mark  ---------------action
//联系我
-(void)contactMe
{
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame=CGRectMake(0, 0, self.window.frame.size.width, self.window.frame.size.height);
    btn.backgroundColor=[UIColor blackColor];
    btn.alpha=0.5;
    [btn addTarget:self action:@selector(removeChat) forControlEvents:UIControlEventTouchUpInside];
    [self.window addSubview:btn];
    
    if (chatView) {
        [[NSNotificationCenter defaultCenter]removeObserver:chatView];
    }
    
    chatView = [[ChatView alloc]initWithFrame:CGRectMake(20, 64, Mainwidth-40, MainHeight-104)];
    chatView.backgroundColor=[UIColor whiteColor];
    chatView.layer.cornerRadius=5;
    [self.window addSubview:chatView];
    
}

-(void)removeChat
{
    [btn removeFromSuperview];
    [chatView removeFromSuperview];
}


@end
